﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Webdiyer.AspNetCore;
using ZhongKeSite.Entity;

namespace ZhongKeSite.Repository
{
    public class ViewRepository<T> : IViewRepository<T> where T : class
    {
        public readonly ZhongKeContext _dbContext;
        public ViewRepository(ZhongKeContext dbContext)
        {
            _dbContext = dbContext;
        }
        
        public virtual T FirstOrDefault()
        {
            return _dbContext.Query<T>().FirstOrDefault();
        }
        public virtual T FirstOrDefault(Expression<Func<T, bool>> predicate)
        {
            return _dbContext.Query<T>().FirstOrDefault(predicate);
        }
        public virtual IEnumerable<T> GetList()
        {
            return _dbContext.Query<T>().AsEnumerable();
        }
        public virtual IEnumerable<T> GetList(Expression<Func<T, bool>> predicate)
        {
            return _dbContext.Query<T>()
                   .Where(predicate)
                   .AsEnumerable();
        }
        public virtual int Count(Expression<Func<T, bool>> predicate)
        {
            return _dbContext.Query<T>()
                   .Count(predicate);
        }
        
        public virtual PagedList<T> GetPagedList(int pageIndex, int pageSize, Expression<Func<T, bool>> predicate)
        {
            return _dbContext.Query<T>()
                   .Where(predicate).
                   ToPagedList<T>(pageIndex, pageSize);
        }
    }
}
